--[[

    Copyright (c) 2011-2020 baby-bus.com

    TODO:   SpriteName
    Author: DemonYao
    Date:   2020.09.21
 
    http://www.babybus.com/ 

]] 




--------------------------
-- 引入命名空间
--------------------------
-- local BaseLayer = import("app.common.layer.BaseLayer")




--[[
    类
--]]


local M = classSprite("dragonBone")
local dbFactory = dragonBones.CCFactory



--------------------------
-- 构造函数
--------------------------

function M:ctor(params)
    -- [超类调用]
    M.super.ctor(self, params)
    -- [本类调用]
    self.dragonBonesDataFile  = params.dragonBonesDataFile      -- 骨骼文件
    self.dragonBonesTexFile   = params.dragonBonesTexFile       -- 骨骼文件
    self.armatureName         = params.armatureName             -- 骨骼名称
    self.animName             = params.animName                 -- 初始骨骼动作名称（选填）
end

--------------------------
-- 渲染
--------------------------

function M:onRender()
    M.super.onRender(self)
    -- [本类调用]
    -- 加载骨骼数据
    self:addDragonBonesData()
    -- 创建骨架
    self:createArmature()
    -- 动作事件监听
    self:bindDBEvent(self.armatureDisplay)
end

-- 加载节点
function M:loadDisplay()
    -- 
end

-- 加载渲染以外的其他操作
function M:onEnterTransitionFinish()
    --触控 

    self:bindTouchLocate()
    -- tool:loadHelpRect({
	-- 	node = self,
	-- 	pos = cc.p(0, 0),
	-- 	size = cc.size(self:cw(), self:ch()),
	-- 	anchorPoint = cc.p(0, 0)
    -- }):opacity(200):z(-1)
    self:moevTo()
end

-- 加载骨骼数据
function M:addDragonBonesData()
    if self.dragonBonesDataFile and self.dragonBonesTexFile then
	    dragonBones.CCFactory:loadDragonBonesData(self.dragonBonesDataFile, "test")
        dragonBones.CCFactory:loadTextureAtlasData(self.dragonBonesTexFile, "test")
    end
end

-- 创建骨骼
function M:createArmature()
    local armatureDisplay = dragonBones.CCFactory:buildArmatureDisplay(self.armatureName)
    CCNodeExtend.extend(armatureDisplay)
    -- 设置节点大小
    self:setContentSize(cc.size(armatureDisplay:cw(), armatureDisplay:ch()))
    armatureDisplay:anchor(cc.p(0.5, 0.5))
    local size = armatureDisplay:getContentSize()
    local anchor = armatureDisplay:getAnchorPoint()
    
    armatureDisplay:to(self):p(size.width * anchor.x, size.height * anchor.y)
    self.armatureDisplay = armatureDisplay

    -- 获取Animation
    self:getAnimation()
    if self.animName then
        self:play(self.animName)
    end
end

function M:moevTo()
    local fromX = self.armatureDisplay:px()
	local fromY = self.armatureDisplay:py()
	local toX =  math.random(1, 100)/100 * V.w - V.w * 0.5;
	local toY =  math.random(1, 100)/100 * V.h - V.h * 0.5;
	local dX = toX - fromX;
    local dY = toY - fromY;
    
	local rootSlot = self:getArmature():getBone("root")
    local bulletSlot = self.armatureDisplay:getArmature():getBone("bullet")

    local rootSlotOffset = rootSlot:getOffset()
    rootSlotOffset:setScaleX(math.sqrt(dX * dX + dY * dY) / 100.0)
    rootSlotOffset:setRotation(math.atan2(dY, dX))
    rootSlotOffset:setSkew(math.random(1, 100)/100 * math.pi - math.pi * 0.5)

    local bulletSlotOffset = bulletSlot:getOffset()
    bulletSlotOffset:setScaleX(0.5 +  math.random(1, 100)/100 * 0.5)
    bulletSlotOffset:setScaleY(0.5 +  math.random(1, 100)/100 * 0.5)

    -- rootSlot.offset.scaleX = math.sqrt(dX * dX + dY * dY) / 100.0
    -- rootSlot.offset.rotation = math.atan2(dY, dX);
    -- rootSlot.offset.skew = math.random(1, 100)/100 * math.pi - math.pi * 0.5
	-- bulletSlot.offset.scaleX = 0.5 +  math.random(1, 100)/100 * 0.5; 
	-- bulletSlot.offset.scaleY = 0.5 +  math.random(1, 100)/100 * 0.5;
	rootSlot:invalidUpdate();
	bulletSlot:invalidUpdate();
    local timeScale = self:getAnimation():getTimeScale()
    -- self:getAnimation().timeScale = 0.5 +  math.random(1, 100)/100 * 1.0
    self:getAnimation():setTimeScale(0.5 +  math.random(1, 100)/100 * 1.0)
	self:play("idle", 1);
end


--------------------------
-- 功能函数
--------------------------
-- 动作播放
function M:play(animationName, playTimes)
    local animation = self.animation
    if animation then
        animation = self:getAnimation()
    end
    playTimes = playTimes or -1
    local state = animation:play(animationName, playTimes)
    return state
end

-- 获取Animation
function M:getAnimation()
    self.animation = self.armatureDisplay:getAnimation()
    return self.animation
end

-- 获取动作状态
function M:getAnimationState(animationName)
    local state = self:getAnimation():getState(animationName)
    return state
end

-- 淡入播放指定的动画。
function M:fadeInplay(animationName, fadeInTime, params)
    params = params or {}
    local playTimes   = params.playTimes or -1
    local layer       = params.layer or 0
    local group       = params.group or "" 
    local fadeOutMode = params.fadeOutMode or 0
    local state = self:getAnimation():fadeIn(animationName, fadeInTime, playTimes, layer, group, fadeOutMode)
    return state
end

-- 获取armture
function M:getArmature()
    return self.armatureDisplay:getArmature()
end

-- 获取骨头
-- param: string  boneName   骨头名称
function M:getBone(boneName)
    local armature = self:getArmature():getBone(boneName)
    -- print("1========", tolua.type(armature))
    -- if armature then
    --     armature:getBone(boneName)
    -- end
    return armature
end














----------------------------------------------------
-- 骨骼事件绑定
----------------------------------------------------
-- 骨骼事件绑定
function M:bindDBEvent(armatureDisplay)
    armatureDisplay:addDBEventListener("start", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("loopComplete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("complete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeIn", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeInComplete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeOut", handler(self, self.animationEventHandler))
 
    armatureDisplay:addDBEventListener("fadeOutComplete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("frameEvent", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("soundEvent", handler(self, self.animationEventHandler))
end

function M:animationEventHandler(eventObject)
    if eventObject.type == "start" then

    elseif eventObject.type == "loopComplete" then

    elseif eventObject.type == "complete" then
        self:moevTo()
    elseif eventObject.type == "fadeIn" then       

    elseif eventObject.type == "fadeInComplete" then

    elseif eventObject.type == "fadeOut" then

    elseif eventObject.type == "fadeOutComplete" then

    elseif eventObject.type == "frameEvent" then

    elseif eventObject.type == "soundEvent" then

    end
end




--------------------------
-- 触控
-------------------------- 

function M:onTouchBegan(x, y, touches)
    --  触控有效
    return SIGN_TOUCH_BEGAN_SWALLOWS
end

function M:onTouchMoved(x, y, touches)
    --  
end

function M:onTouchEnded(x, y, touches)
    --  
end







--------------------------
-- 属性
--------------------------




--------------------------
-- 父类重写
--------------------------




----------------------------------------------------
-- 析构
--------------------------

function M:onDestructor()
    -- [超类调用]
M.super.onDestructor(self)
    -- [本类调用]

end








return M






